﻿namespace WMS6.Web.Administration
{
    using System;
    using System.Collections.Generic;
    using System.Web.Caching;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.LoginServiceRef;
    using WMS6.Web.CommonMethodServiceRef;

    public class GlobalSetting : BasePage
    {
        protected HtmlAnchor A1;
        protected HtmlForm F1085;
        protected GridView gvGlobalSetting;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected HtmlHead Head1;
        protected Label lblGlobalSetting;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        protected void CacheRemove(object sender, EventArgs e)
        {
            base.Cache.Remove("FUNCTION_ROLE");
            this.Session.Remove("RoleFunctionData");
            base.Cache.Remove("StringOper_CN");
            base.Cache.Remove("StringOper_EN");
            base.Cache.Remove("StringOper_TW");
            base.Cache.Remove("DateTimeOper_CN");
            base.Cache.Remove("DateTimeOper_EN");
            base.Cache.Remove("DateTimeOper_TW");
            base.Cache.Remove("EqualDLLByDT");
            base.Cache.Remove("GLOBAL_SETTING");
            List<WareHouseName4DDL> wareHouseName4DDLList = CommoneMethodServiceClientProxy.GetInstance().GetWareHouseDataSource();
            if ((wareHouseName4DDLList != null) && (wareHouseName4DDLList.Count > 0))
            {
                for (int i = 0; i < wareHouseName4DDLList.Count; i++)
                {
                    base.Cache.Remove(wareHouseName4DDLList[i].WhDbID + "SysCode");
                }
            }
            base.Cache.Remove("Employee");
            this.lblMsg.Text = "缓存释放成功!";
        }

        protected void gvGlobalSetting_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            base.Response.Redirect("GlobalSetting.aspx");
        }

        protected void gvGlobalSetting_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblDescr"));
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
            }
        }

        protected void gvGlobalSetting_RowEditing(object sender, GridViewEditEventArgs e)
        {
            e.Cancel = true;
            this.gvGlobalSetting.EditIndex = e.NewEditIndex;
            this.WebConfigLoad();
        }

        protected void gvGlobalSetting_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                int index = this.gvGlobalSetting.EditIndex;
                GridViewRow gvr = this.gvGlobalSetting.Rows[index];
                GlobalSettingInfo info = new GlobalSettingInfo {
                    GlobalSettingID = ((TextBox) gvr.Cells[2].Controls[1]).Text,
                    Descr = ((TextBox) gvr.Cells[3].Controls[1]).Text,
                    Value = ((TextBox) gvr.Cells[4].Controls[1]).Text,
                    UpdatedBy = base.UserInfo.UserID
                };
                this.lblMsg.Text = LoginServiceClientProxy.GetInstance().UpdateGlobalSetting(info);
                base.Cache.Remove("GLOBAL_SETTING");
                base.Response.Redirect("GlobalSetting.aspx");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.WebConfigLoad();
            }
        }

        private void WebConfigLoad()
        {
            try
            {
                List<GlobalSettingInfo> globalSettingList = null;
                if (base.Cache.Get("GLOBAL_SETTING") == null)
                {
                    globalSettingList = LoginServiceClientProxy.GetInstance().GetALLGlobalSetting();
                    base.Cache.Add("GLOBAL_SETTING", globalSettingList, null, DateTime.Now.AddHours(12.0), TimeSpan.Zero, CacheItemPriority.High, null);
                }
                else
                {
                    globalSettingList = base.Cache.Get("GLOBAL_SETTING") as List<GlobalSettingInfo>;
                }
                base.BindData2GridView(this.gvGlobalSetting, globalSettingList);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = "操作异常：" + ex.Message;
            }
        }
    }
}

